package image;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.UUID;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import basicShapes.Arc;
import basicShapes.Ellipse;
import basicShapes.Line;
import basicShapes.Point;
import basicShapes.Polygon;
import basicShapes.Rectangle;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "image", propOrder = {
	"name",	
    "shapes",
    "owner"
})
@XmlRootElement(name = "image")
public class Image {

	@XmlAttribute
	protected String name;
	@XmlElementRef
	private ArrayList<Image> shapes;
	@XmlTransient
	private BufferedImage bi;
	@XmlAttribute
	private String owner;
	
	public Image() {
		this.name = UUID.randomUUID().toString();
		this.shapes = new ArrayList<Image>();
		this.bi = new BufferedImage(500,500, BufferedImage.TYPE_INT_RGB);
	}

	public Image(String name, String owner) {
		File f = new File(name);
		if(!f.exists()) {
			this.name = name;
			this.shapes = new ArrayList<Image>();
			this.bi = new BufferedImage(500,500, BufferedImage.TYPE_INT_RGB);
			this.owner = owner;
			try {
				/* MARSHALLER JAVA TO XML */
				JAXBContext context = JAXBContext.newInstance(Image.class, 
																Line.class,
																Ellipse.class,
																Arc.class,
																Polygon.class,
																Rectangle.class,
																Point.class);
				Marshaller m = context.createMarshaller();
				m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
				m.marshal(this, new File(this.name+".xml"));
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void drawImage() {
		Graphics2D g = bi.createGraphics();
		for(Image img : this.shapes) {
			img.draw(g);
		}
	}
	
	public void draw(Graphics2D g) {
		for(Image img : shapes) {
			img.draw(g);
		}
	}
	
	public void addArc(int x, int y, int w, int h, int s, int e, int t) {
		shapes.add(new Arc(x, y, w, h, s, e, t));
	}
	
	public void addEllipse(int x, int y, int w, int h) {
		shapes.add(new Ellipse(x, y, w, h));
	}

	public void addLine(int x1, int y1, int x2, int y2) {
		shapes.add(new Line(x1, y1, x2, y2));
	}

	public void addPolygon(int pointsX[], int pointsY[], int size) {
		System.out.println("--> size = "+size);
		shapes.add(new Polygon(pointsX, pointsY, size));
	}

	public void addRectangle(int x, int y, int w, int h) {
		shapes.add(new Rectangle(x, y, w, h));
	}

	public void addImage(Image i) {
		shapes.add(i);
	}

	public ArrayList<Image> getShapes() {
		return shapes;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public BufferedImage getBi() {
		return bi;
	}

	public void setShapes(ArrayList<Image> shapes) {
		this.shapes = shapes;
	}

	public String getOwner() {
		return owner;
	}
	
	public String toString(){
		return "Image - name:"+this.name;
	}
}
